<?php
class sfFoxsisMenu extends sfWebRequest
{
    private $menu_yml    = 'menu.yml';
    private $config_menu = array();
    private $menus       = array();
    private $sf_user;

    public function __construct($sf_user)
    {
    	$this->sf_user = $sf_user;
        $arquivoYml = sfConfig::get('sf_FoxsisMenu_yml') . $this->menu_yml;
        if(!file_exists($arquivoYml)) {
            trigger_error("Arquivo de configuração do menu '$arquivoYml' não encontrado");
        }
        $yf = sfYaml::load( sfConfig::get('sf_FoxsisMenu_yml') . $this->menu_yml );
        //$this->config_menu = $yf['config'];
        $this->menus       = $yf['menus'];
    }

    public function render()
    {
        $retorno = substr($this->criarMenus($this->menus), 0, -10);
        $html_menu = "<script type='text/javascript'>\n";
        $html_menu.= "var menu = [\n";
        $html_menu.= $retorno;
        $html_menu.= "]\n";
        $html_menu.= "cmDraw ('menu', menu, 'hbr', cmThemeOffice, 'ThemeOffice');;\n";
        $html_menu.= "</script>";

        echo $html_menu;
    }

    private function criarMenus($arr_menus, $parent = true)
    {
        $retorno = "";
        $html_menu = '';

        foreach ($arr_menus as $menu)
        {
        	$bol_permitido = $this->sf_user->canAccess(
	        	$this->getArrayValue($menu, 'module', false),
	        	$this->getArrayValue($menu, 'action', false)
	      	);

	      	if ($bol_permitido)
	      	{
	            if($parent === false)
	                $retorno .= "\t";

	            $retorno.= "[";
	            if($this->getArrayValue($menu, 'image', false) != "")
	                $retorno.= "'". image_tag('menu/'.$this->getArrayValue($menu, 'image', false)) ."',";
	            else
	                $retorno.= $this->getArrayValue($menu, 'image') . ",";

	            // nome
	            $retorno.= $this->getArrayValue($menu, 'text') .",";

	            // url
	            $url = "";
	            $module = $this->getArrayValue($menu, 'module', false);
	            $action = $this->getArrayValue($menu, 'action', false);
	            $link = $this->getArrayValue($menu, 'link', false);

	            if ($module || $action){
	            	if($action == "")
	            		$action  = 'index';

	                $url = url_for("$module/$action");
	            }
	            if($link != "")
	            	$url = $link;

	            $retorno.= "'". $url ."',";

	            // target
	            $retorno.= $this->getArrayValue($menu, 'target') .",";
	            // descricao
	            $retorno.= $this->getArrayValue($menu, 'description');

	            if(isset($menu['submenus'])) {
	            	$submenus = $this->criarMenus($menu['submenus'], false);
	            	$retorno .= ",\n". $submenus;
	            }

	            $retorno.= "],\n";

	            if($parent === true)
	            	$retorno.= "_cmSplit,\n";
	      	}
        }

        return $retorno;
    }

    private function getArrayValue($array, $key, $addAspas = true)
    {
        $result = '';
        if (isset($array[$key])){
            $result = $array[$key];
        }

        return $addAspas ? $this->addAspas($result) : $result;
    }

    private function addAspas($string)
    {
        if ($string === ''){
            return "''";
        } else {
            return is_numeric($string) ? $string : "'$string'";
        }
    }
}